package com.dme.poi.thread;

import com.dme.poi.entity.People;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@SuppressWarnings("all")
public class ThreadTest implements Runnable{
    private List<People> dataLists;
    private CountDownLatch countDownLatch;
    private SXSSFWorkbook workbook;
    private Integer columnNumbers;
    private Integer startIndex;

    public ThreadTest(List<People> dataLists, CountDownLatch countDownLatch, SXSSFWorkbook workbook, Integer columnNumbers, Integer startIndex) {
        this.dataLists = dataLists;
        this.countDownLatch = countDownLatch;
        this.workbook = workbook;
        this.columnNumbers = columnNumbers;
        this.startIndex = startIndex;
    }

    @Override
    public void run() {
        try {
            doExport();
        } catch (IllegalAccessException | IOException e) {
            e.printStackTrace();
        }
    }

    private void doExport() throws IllegalAccessException, IOException {
        long startTime = new Date().getTime();
        Iterator<People> iterator = dataLists.iterator();
        System.out.println(Thread.currentThread().getName() + "操作的数据量为:"+dataLists.size());
        Field[] fields = People.class.getDeclaredFields();
        Integer currentStart = 0;
        SXSSFSheet sheet = workbook.createSheet();
        if (currentStart == 0){
            Row row = sheet.createRow(currentStart);
            for (int i = 0; i < fields.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(fields[i].getName());
            }
            currentStart++;
        }
        while (iterator.hasNext()){
            People people = iterator.next();
            Row row = sheet.createRow(currentStart);
            for (int i = 0; i < fields.length; i++) {
                if (i != fields.length-1){
                    Cell cell = row.createCell(i);
                    cell.setCellValue(String.valueOf(fields[i].get(people)));
                }else {
                    doImageExport(i,currentStart,workbook,sheet);
                }
            }
            currentStart++;
        }
        long startTime_ = new Date().getTime();
        System.out.println(Thread.currentThread().getName() + "开始写入文件.....");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\Desktop\\test\\"+Thread.currentThread().getName()+"-2021-10-19-POI.xlsx"));
        workbook.write(fileOutputStream);
        System.out.println(Thread.currentThread().getName() + "文件写入完毕... -- 写入文件消耗时间 = " + (new Date().getTime() - startTime_));
        countDownLatch.countDown();
        System.out.println(Thread.currentThread().getName() + "完成任务，执行所消耗的时间为 == " + ( new Date().getTime() - startTime));
    }


    public static void doImageExport(Integer col, Integer row, Workbook wb, Sheet sheet){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\1.jpg");
            byte[] bytes = IOUtils.toByteArray(fileInputStream);
            CreationHelper creationHelper = wb.getCreationHelper();
            Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
            ClientAnchor clientAnchor = creationHelper.createClientAnchor();
            clientAnchor.setCol1(col);
            clientAnchor.setRow1(row);
            Picture picture = drawingPatriarch.createPicture(clientAnchor, wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG));
            picture.resize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
